//
//  IFScheduSchedulTimeView.m
//  Medicare
//
//  Created by admin on 2017/8/8.
//  Copyright © 2017年 medicare. All rights reserved.
//

#import "IFSchedulTimeView.h"
#import "IFNewPatientRoundsTwoCell.h"
#import "ZSchedulingTypeModel.h"
#import "IFMySchedulingPublicDefines.h"
@interface IFSchedulTimeView ()<UICollectionViewDataSource,
UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> {
    
    

}

@end

@implementation IFSchedulTimeView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.selectArray = [NSMutableArray arrayWithCapacity:0];
        self.dataSourArray = [NSMutableArray arrayWithCapacity:0];
       // [self requestDataList];
        self.backgroundColor = [UIColor redColor];
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        [self addSubview:self.collectionView];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
       // self.collectionView.c = self;
        self.collectionView.scrollEnabled = NO;
        [self.collectionView registerClass:[MySelectNameCollectionCell class] forCellWithReuseIdentifier:@"GradientCell"];
        [self.collectionView registerClass:[MySelectNameCollectionCell class] forCellWithReuseIdentifier:@"GradientTwoCell"];
    }
    return self;
}

#pragma mark - 网络请求
- (void)requestDataList {
  
    [[IFly_APIManager shareManager] api_getClassInfoWithDeptCode:[UserService share].currentAccount.selectedDeptCode andBackBlock:^(BackDataVO *data, NSError *error) {
        if ([data.s isEqualToString:@"1"]) {
            self.dataSourArray = [ZSchedulingTypeModel mj_objectArrayWithKeyValuesArray:data.r];
            [self.collectionView reloadData];
        } else {
           
        }
    }];
}
/*
"classInfoId\": \"1\",
"deptCode\": \"1011\",
\"className\": \"白班\",
\"color\": \"white\",
\"createId\": \"55232\",
\"createTime\": \"2016-09-1411: 03: 17\",
\"opId\": \"22683\",
\"opTime\": \"2017-09-2411: 03: 17\"
*/

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourArray.count;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSourArray.count <= 5) {
        return CGSizeMake((self.collectionView.frame.size.width - 10 - 4 * 5)/ 5.f, self.collectionView.frame.size.height - 30);
    } else {
        return CGSizeMake((self.collectionView.frame.size.width - 10 - 4 * 5)/ 5.f, (self.collectionView.frame.size.height- 10 - 8)/2.f );
    }
}

// 设置最小列间距，也就是左行与右一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

// 设置最小行间距，也就是前一行与后一行的中间最小间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 8;
}

//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (self.dataSourArray.count <= 5) {
        
       
         return UIEdgeInsetsMake(15, 5, 15, 5);
        
    } else {
        
      
      return UIEdgeInsetsMake(5, 5, 5, 5);
    }
   
}
//26 14
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier;
    if (self.dataSourArray.count <= 5) {
        CellIdentifier = @"GradientCell";
        
    } else {
        CellIdentifier = @"GradientTwoCell";
    }
    MySelectNameCollectionCell * cell
    = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    ZSchedulingTypeModel * model = self.dataSourArray[indexPath.row];
    cell.titleLabel.textAlignment = NSTextAlignmentCenter;
    if (model.className.length >3) {
    cell.titleLabel.text =  [model.className substringToIndex:3];
    } else {
    cell.titleLabel.text = model.className;
    }
    cell.titleLabel.textColor =  COLORDIC[model.color];
    cell.titleLabel.layer.borderWidth = 0.5f;
    cell.titleLabel.layer.cornerRadius = 3.f;
    cell.titleLabel.layer.masksToBounds = YES;
    cell.titleLabel.schedulingTypeModel = model;
    //cell.titleLabel.layer.borderColor = [UIColor colorWithHexString:@"d4d4d4"].CGColor;
    if ([self.selectArray containsObject:model]) {
        
      cell.isSelect = YES;
        
    } else {
       cell.isSelect = NO;
 
    }
    [cell setNeedsLayout];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isClickItem) {
        [SVProgressHUD showTextHUD:@"请先选择要修改的班日"];
        return ;
    }
    ZSchedulingTypeModel *typeModel = self.dataSourArray[indexPath.row];
    MySelectNameCollectionCell * cell = (MySelectNameCollectionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    if ([self.selectArray containsObject:typeModel]) {
        [self.selectArray removeObject:typeModel];
    }
    if (self.selectArray.count >= 2) {
        [SVProgressHUD showTextHUD:@"每天最多只能选择两个班次"];
        return ;
    }
    if ([self.delegate respondsToSelector:@selector(selectSchedulingTimeClick: SelectNameCollectionCellModel:)] && self.delegate) {
        if (cell.isSelect) {
            cell.isSelect = NO;
            //[self.selectArray removeObject:cell.titleLabel.text];
             [self.delegate selectSchedulingTimeClick:@"" SelectNameCollectionCellModel:typeModel];
        } else {
            cell.isSelect = YES;
            
            [self.selectArray addObject:typeModel];
            [self.delegate selectSchedulingTimeClick:typeModel.className SelectNameCollectionCellModel:typeModel];
        }
       
    }
    
}


- (void)reloatData:(NSMutableArray *)selectTitleArray {
    NSMutableArray * array = [NSMutableArray arrayWithArray:selectTitleArray];
    for (ZSchedulingTypeModel * str  in array) {
        if (![self.dataSourArray containsObject:str]) {
            [selectTitleArray removeObject:str];
        }
       
    }
    self.selectArray = [NSMutableArray arrayWithArray:selectTitleArray];
    [self.collectionView reloadData];
    
}
//重写数据源数组的set方法
- (void)setDataSourArray:(NSMutableArray *)dataSourArray {
    if (_dataSourArray != dataSourArray) {
        _dataSourArray = dataSourArray;
    }
    NSMutableArray * array = [NSMutableArray arrayWithArray:self.selectArray];
    for (ZSchedulingTypeModel * typeModel  in array) {
        if (![self.dataSourArray containsObject:typeModel]) {
            [_selectArray removeObject:typeModel];
        }
        
    }
}




@end
